package com.bw.ivideo

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.bw.ivideo.databinding.ActivityTestBinding
import com.bw.ivideo.databinding.ItemTestBinding
import com.ethanhua.skeleton.Skeleton

class TestActivity : AppCompatActivity() {
    private lateinit var binding:ActivityTestBinding
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityTestBinding.inflate(layoutInflater)
        setContentView(binding.root)

        initRecycleView()


    }

    private fun initRecycleView() {
        binding.rvTest.layoutManager=LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false)
        val list = listOf<String>("11","22","33","44","55","11","22","33","44","55","11","22","33","44","55")
        val adapter = TestAdapter(list)

        binding.rvTest.adapter = adapter
        adapter.notifyDataSetChanged()

        val skeleton = Skeleton.bind(binding.rvTest)
            .adapter(adapter)//设置实际adapter
            .shimmer(true)//是否开启动画
            .angle(30)//shimmer的倾斜角度
            .color(com.bw.common.R.color.black)//shimmer的颜色
            .frozen(true)//true则表示显示骨架屏时，RecyclerView不可滑动，否则可以滑动
            .duration(1200)//动画时间，以毫秒为单位
            .count(10)//显示骨架屏时item的个数
            .load(R.layout.item_test)//骨架屏UI
            .show(); //default count is 10

        skeleton.show()



        binding.rvTest.postDelayed({
                                   skeleton.hide()
        },2000)

    }

    class TestAdapter(val list:List<String>):RecyclerView.Adapter<TestAdapter.TestViewHolder>(){
        class TestViewHolder(view:View):RecyclerView.ViewHolder(view){
            val itemBinding:ItemTestBinding by lazy {
                ItemTestBinding.bind(view)
            }
        }

        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): TestViewHolder {
            val view = LayoutInflater.from(parent.context).inflate(R.layout.item_test,parent,false)
            return TestViewHolder(view)
        }

        override fun getItemCount(): Int {
            return list.size
        }

        override fun onBindViewHolder(holder: TestViewHolder, position: Int) {
            val value = list.get(position)
            holder.itemBinding.tvItemTest.text=value
        }
    }
}